package com.fone.player.play.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.VgdetailRst;
import com.fone.player.client.VgdetailRst.From;
import com.fone.player.play.ChangeVideoEvent;
import com.fone.player.play.VideoPlayerMessage;
import com.fone.player.play.fragment.VideoCommentFragment;
import com.fone.player.play.fragment.VideoIntroductionFragment;
import com.fone.player.play.fragment.VideoRelatedFragment;
import com.fone.player.play.fragment.VideoSeriesFragment;
import com.fone.player.util.L;

import de.greenrobot.event.EventBus;

/**
 * 剧集模式视频详情页适配器
 * 
 * @author kongxiaojun
 * @since 2014-4-18
 * 
 */
public class SeriesModeAdapter extends FragmentStatePagerAdapter {

	private final int[] TITLES = { R.string.video_info_tab_introduction, R.string.video_info_tab_series, R.string.video_info_tab_comment, R.string.video_info_tab_relate };
	private String currentPlayUrl;
	private String dramaurl;
	private String addcommenturl;
	private String commenturl;
	private String linkurl;

	public SeriesModeAdapter(FragmentManager fm, String dramaurl, String currentPlayUrl, String addcommenturl, String commenturl, String linkurl) {
		super(fm);
		this.currentPlayUrl = currentPlayUrl;
		this.addcommenturl = addcommenturl;
		this.commenturl = commenturl;
		this.linkurl = linkurl;
		this.dramaurl = dramaurl;
		EventBus.getDefault().register(this);
	}

	@Override
	public CharSequence getPageTitle(int position) {
		return ApplicationManage.getAplicationManageInstance().getString(TITLES[position]);
	}

	@Override
	public int getCount() {
		return TITLES.length;
	}

	@Override
	public Fragment getItem(int position) {

		Fragment fragment = null;

		switch (position) {
		case 0:
			fragment = new VideoIntroductionFragment();
			break;
		case 1:
			fragment = VideoSeriesFragment.newInstances(dramaurl, currentPlayUrl);
			break;
		case 2:
			fragment = VideoCommentFragment.newInstances(addcommenturl, commenturl);
			break;
		case 3:
			fragment = VideoRelatedFragment.newInstances(linkurl);
			break;
		default:
			break;
		}
		return fragment;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	/**
	 * eventbus事件
	 * 
	 * @return void
	 * @throws
	 */
	public void onEvent(VideoPlayerMessage msg) {
		switch (msg.what) {
		case VideoPlayerMessage.MESSGAE_CHANGE_VIDEO:// 切换视频
			ChangeVideoEvent event = (ChangeVideoEvent) msg.obj;
			this.currentPlayUrl = event.xyzPlayUrl;
			break;
		case VideoPlayerMessage.MESSGAE_CHANGE_FROM:// 切换来源
			From from = (From) msg.obj;
			this.currentPlayUrl = from.defaulturl;
			this.dramaurl = from.dramaurl;
			break;
		default:
			break;
		}
	}
	
	public void destory(){
		EventBus.getDefault().unregister(this);
	}

}
